clear all
set more off

global datapath /INSERTPATH
global codepath /INSERTPATH
global resultspath /INSERTPATH

local countrysetname all

/* load dataset */
use "$datapath/full_dataset.dta", clear

/* additional cleaning steps */ 
run "$codepath/prep_regression_amadeus.do"

/* create ids and variables */
egen firm_id = group(bvdid)
egen county_id = group(county_unique)
encode country, gen(countrycode)

gen ln_revenue = ln(revenue)
gen ln_numberofemployees = ln(numberofemployees)
gen ln_revenue_worker = ln(revenue/numberofemployees)
gen ln_gdp_pc = ln(gdp_per_capita_real_PPP)

keep if ~missing(ln_revenue_worker)

/* generate sampling weights for some specifications */
bysort country: gen samplingweights = 1/[_N]
gen GDP_and_sampling_weights = samplingweights*world_GDP_share
gen emp_and_sampling_weights = samplingweights*world_employment_share

/* choose set of countries, temperature transformation, sector, dependent variable, interaction, and weighting */
local countrysetname all
local transforms polyabove1 /*polyabove2 bins poly4*/
local sectors manu /*serv all*/
local lhsvars ln_revenue_worker /*ln_revenue ln_numberofemployees numberofemployees*/
local interactions interacted /*uninteracted tripleinteracted*/
local weightings GDP pop unweighted 
local fe country_year_fe state_year_fe
local weather_controls precip_only temp_lags precip_plus_lags precip_temp_both_lagged /**/

/* assemble rhs variables according to options chosen above */ 

foreach weights of local weightings {
foreach answer of local interactions {
foreach transform of local transforms {
foreach weather of local weather_controls {
if "`transform'" == "bins" & "`answer'" == "uninteracted" {
local rhsvars bin_5C_35_Inf bin_5C_30_35 bin_5C_25_30 ///
bin_5C_20_25 bin_5C_10_15 bin_5C_5_10 bin_5C_0_5 bin_5C_n5_0 bin_5C_n10_n5 bin_5C_nInf_n10
}
if "`transform'" == "bins" & "`answer'" == "interacted" {
local rhsvars bin_5C_35_Inf bin_5C_30_35 bin_5C_25_30 ///
bin_5C_20_25 bin_5C_10_15 bin_5C_5_10 bin_5C_0_5 bin_5C_n5_0 bin_5C_n10_n5 bin_5C_nInf_n10 ///
c.bin_5C_35_Inf#c.avg_tmax_lr c.bin_5C_30_35#c.avg_tmax_lr c.bin_5C_25_30#c.avg_tmax_lr ///
c.bin_5C_20_25#c.avg_tmax_lr c.bin_5C_10_15#c.avg_tmax_lr c.bin_5C_5_10#c.avg_tmax_lr ///
c.bin_5C_0_5#c.avg_tmax_lr c.bin_5C_n5_0#c.avg_tmax_lr c.bin_5C_n10_n5#c.avg_tmax_lr ///
c.bin_5C_nInf_n10#c.avg_tmax_lr ///
c.bin_5C_35_Inf#c.ln_gdp_pc c.bin_5C_30_35#c.ln_gdp_pc ///
c.bin_5C_25_30#c.ln_gdp_pc c.bin_5C_20_25#c.ln_gdp_pc ///
c.bin_5C_10_15#c.ln_gdp_pc c.bin_5C_5_10#c.ln_gdp_pc ///
c.bin_5C_0_5#c.ln_gdp_pc c.bin_5C_n5_0#c.ln_gdp_pc ///
c.bin_5C_n10_n5#c.ln_gdp_pc c.bin_5C_nInf_n10#c.ln_gdp_pc
}
if "`transform'" == "poly1" {
local rhsvars poly_1
}
if "`transform'" == "poly2" {
local rhsvars poly_1 poly_2
}
if "`transform'" == "poly3" {
local rhsvars poly_1 poly_2 poly_3
}
if "`transform'" == "poly4" & "`answer'" == "uninteracted" {
local rhsvars poly_1 poly_2 poly_3 poly_4
}
if "`transform'" == "poly4" & "`answer'" == "interacted" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr
}
if "`transform'" == "poly4" & "`answer'" == "interacted" & "`weather'" == "precip_only" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr ///
prcp_poly_1 prcp_poly_2
}
if "`transform'" == "poly4" & "`answer'" == "interacted" & "`weather'" == "temp_lags" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr ///
poly_1_l1 poly_2_l1 poly_3_l1 poly_4_l1 ///
poly_1_l2 poly_2_l2 poly_3_l2 poly_4_l2 ///
poly_1_l3 poly_2_l3 poly_3_l3 poly_4_l3 ///
poly_1_l4 poly_2_l4 poly_3_l4 poly_4_l4 
}
if "`transform'" == "poly4" & "`answer'" == "interacted" & "`weather'" == "precip_plus_lags" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr ///
poly_1_l1 poly_2_l1 poly_3_l1 poly_4_l1 ///
poly_1_l2 poly_2_l2 poly_3_l2 poly_4_l2 ///
poly_1_l3 poly_2_l3 poly_3_l3 poly_4_l3 ///
poly_1_l4 poly_2_l4 poly_3_l4 poly_4_l4 ///
prcp_poly_1 prcp_poly_2
}
if "`transform'" == "poly4" & "`answer'" == "interacted" & "`weather'" == "precip_temp_both_lagged" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr ///
poly_1_l1 poly_2_l1 poly_3_l1 poly_4_l1 ///
poly_1_l2 poly_2_l2 poly_3_l2 poly_4_l2 ///
poly_1_l3 poly_2_l3 poly_3_l3 poly_4_l3 ///
poly_1_l4 poly_2_l4 poly_3_l4 poly_4_l4 ///
prcp_poly_1 prcp_poly_1_l1 prcp_poly_1_l2 prcp_poly_1_l3 prcp_poly_1_l4 ///
prcp_poly_2 prcp_poly_2_l1 prcp_poly_2_l2 prcp_poly_2_l3 prcp_poly_2_l4
}
if "`transform'" == "poly4" & "`answer'" == "tripleinteracted" {
local rhsvars poly_1 poly_2 poly_3 poly_4 ///
c.poly_1#c.ln_gdp_pc c.poly_2#c.ln_gdp_pc ///
c.poly_3#c.ln_gdp_pc c.poly_4#c.ln_gdp_pc ///
c.poly_1#c.avg_tmax_lr c.poly_2#c.avg_tmax_lr ///
c.poly_3#c.avg_tmax_lr c.poly_4#c.avg_tmax_lr ///
c.poly_1#c.ln_gdp_pc#c.avg_tmax_lr c.poly_2#c.ln_gdp_pc#c.avg_tmax_lr ///
c.poly_3#c.ln_gdp_pc#c.avg_tmax_lr c.poly_4#c.ln_gdp_pc#c.avg_tmax_lr 
}
if "`transform'" == "polyabove1" & "`answer'" == "uninteracted" {
local rhsvars polyBelow5_1 polyAbove30_1
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" & "`weather'" == "precip_only" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr ///
prcp_poly_1 prcp_poly_2
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" & "`weather'" == "temp_lags" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr ///
polyBelow5_1_l1 polyBelow5_1_l2 polyBelow5_1_l3 polyBelow5_1_l4 ///
polyAbove30_1_l1 polyAbove30_1_l2 polyAbove30_1_l3 polyAbove30_1_l4
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" & "`weather'" == "precip_plus_lags" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr ///
prcp_poly_1 prcp_poly_2 ///
polyBelow5_1_l1 polyBelow5_1_l2 polyBelow5_1_l3 polyBelow5_1_l4 ///
polyAbove30_1_l1 polyAbove30_1_l2 polyAbove30_1_l3 polyAbove30_1_l4
}
if "`transform'" == "polyabove1" & "`answer'" == "interacted" & "`weather'" == "precip_temp_both_lagged" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr ///
prcp_poly_1 prcp_poly_1_l1 prcp_poly_1_l2 prcp_poly_1_l3 prcp_poly_1_l4 ///
prcp_poly_2 prcp_poly_2_l1 prcp_poly_2_l2 prcp_poly_2_l3 prcp_poly_2_l4 ///
polyBelow5_1_l1 polyBelow5_1_l2 polyBelow5_1_l3 polyBelow5_1_l4 ///
polyAbove30_1_l1 polyAbove30_1_l2 polyAbove30_1_l3 polyAbove30_1_l4
}
if "`transform'" == "polyabove1" & "`answer'" == "tripleinteracted" {
local rhsvars polyBelow5_1 polyAbove30_1 ///
c.polyBelow5_1#c.ln_gdp_pc c.polyAbove30_1#c.ln_gdp_pc ///
c.polyBelow5_1#c.avg_tmax_lr c.polyAbove30_1#c.avg_tmax_lr ///
c.polyBelow5_1#c.ln_gdp_pc#c.avg_tmax_lr c.polyAbove30_1#c.ln_gdp_pc#c.avg_tmax_lr
}
if "`transform'" == "polyabove2" & "`answer'" == "uninteracted" {
local rhsvars polyBelow5_2 polyAbove30_2
}
if "`transform'" == "polyabove2" & "`answer'" == "interacted" {
local rhsvars polyBelow5_2 polyAbove30_2 ///
c.polyBelow5_2#c.ln_gdp_pc c.polyAbove30_2#c.ln_gdp_pc ///
c.polyBelow5_2#c.avg_tmax_lr c.polyAbove30_2#c.avg_tmax_lr
}


if "`fe'" == "country_year_fe" {
	local location_fixed_effect countrycode#year
}

if "`fe'" == "state_year_fe" {
	local location_fixed_effect countrycode#year
}


/* run regressions */ 

foreach sector of local sectors {
foreach lhsvar of local lhsvars {
if "`sector'" == "all" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker), ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'_`weather'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker) & ~missing(avg_tmax_lr) ///
& ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`weights'_`time_fe'_`weather'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if ~missing(ln_revenue_worker) & ~missing(avg_tmax_lr) ///
& ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
}
if "`sector'" == "manu" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker), ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if main_category == "Manufacturing" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
}
if "`sector'" == "serv" {
if "`weights'" == "unweighted" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker), ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
if "`weights'" == "GDP" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=GDP_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
if "`weights'" == "pop" {
reghdfe `lhsvar' `rhsvars' if main_category == "Services" & ~missing(ln_revenue_worker) ///
& ~missing(avg_tmax_lr) & ~missing(ln_gdp_pc) [aw=emp_and_sampling_weights], ///
a(firm_id countrycode#year) cl(firm_id county_id#year)
estimates save "$resultspath/ster/`countrysetname'_`sector'_`transform'_`answer'_`lhsvar'_`time_fe'_`weights'_`weather'.ster", replace
}
}
}
}
}
}
}

